layui.use(['form','laydate','upload'], function () {
    var table = layui.table,
        form = layui.form,
        upload = layui.upload,
        laydate = layui.laydate;

    laydate.render({
        elem: '#createDate',
        type: 'date'
    });

    var uploadInst = upload.render({
        elem: '#test1'
        , url: '/tp/upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            console.log(res.src);
            $("#test1").attr("lay-url",res.src)
            //如果上传失败
            if (res.code > 0) {
                return layer.msg('上传失败');
            }
            //上传成功的一些操作
            //……
            //……
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传成功</span>');
            var fileUpdate = $('.image');
            fileUpdate.attr("value",res.src);//
            console.log("新传的图片的value值"+fileUpdate.attr("value"));
            $('#demoText').html(''); //置空上传失败的状态
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        console.log(data.field);

        let attr = $("#test1").attr("lay-url");
        var s = {
            mainImage:attr
        }
        $.ajax({
            type:'post',
            url: '/JsProduct/advanced/addrizhi',
            data:{operatelist:'商品列表',operatetype:'修改',operateremark:'修改商品信息'},
            success:function(res){
                if(top.window['refresh']){
                    top.window['refresh'].frames.location.reload();
                }
            }
        })
        //修改
        //ajax请求
        $.ajax({
            type: 'post',
            url: '/JsProduct/advanced/update',
            data: data.field,
            dataType: 'json',
            success: function (res) {
                if (res.state == 200){
                    var index = parent.layer.getFrameIndex(window.name);
                    layer.msg(res.msg, {time: 500, icon: 6});
                    alert("修改成功");
                    parent.layer.close(index);
                }else {
                    layer.msg(res.msg, {time: 500, icon: 6});
                    alert("修改失败");
                }
                parent.ajaxtable();
            },
            error: function (res) {
                alert("修改失败");
            }
        })

        return false;
    });



});